
#include "comLogManager.h"
#include "templateLuaMgr.h"

#include <map>
using namespace std;

#include <boost/foreach.hpp>


templateLuaMgr::templateLuaMgr(const string &tt_home)
:m_L(NULL)
,m_TThome(tt_home)
{
	int ret = 0;
//	ttcode_log << endl;
	m_Home = m_TThome + "/tt-app/luaScript";
	m_L = lua_open();
	luaL_openlibs(m_L);
	
	string initLuaStr;
	initLuaStr = "package.cpath = package.cpath..\";";
	initLuaStr += m_TThome + "/tt-app/lib/?.dll\"";
	ret = luaL_dostring(m_L,initLuaStr.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}

	initLuaStr = "package.path = package.path..\";";
	initLuaStr += m_Home + "/?.lua\"";
	ret = luaL_dostring(m_L,initLuaStr.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}

	ret = luaL_dostring(m_L,"io.output(\"ttcode.lua.log\")");
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}
}

templateLuaMgr::~templateLuaMgr()
{
	lua_close(m_L);
}

void templateLuaMgr::callLuaApp(const string &name)
{
	int ret = luaL_dofile(m_L,name.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}
}

void templateLuaMgr::callLuaString(const string &script)
{
	int ret = luaL_dostring(m_L,script.c_str());
	if( ret )
	{
		const char *msgError = lua_tostring(m_L, -1);
		ttcode_log << "msgError=<" << msgError << ">" << endl;
	}
}

